<?php

	class Fmgr0v0r01_FileInfo{
		
		static function read($fp){
			$basename = basename($fp);
			$xtn = SmuoiMime::xtn_of($basename);
			$host_mime = SmuoiMime::read_file($fp);
			$xpct_mime = SmuoiMime::xtn_switch($xtn);
			$a = array_shift(explode('/', $host_mime));
			$b = array_shift(explode('/', $xpct_mime));
			if(($b !== 'application') && ($a !== $b)) $host_mime = $xpct_mime;
			$mime = self::mime_of($host_mime, $xtn);
			return array(
				'host_mime' => $host_mime,
				'mime' => $mime,
				'class' => self::class_of($mime, $basename, $xtn),
				'filesize' => filesize($fp),
				'checksum' => md5_file($fp),
				'xtn' => $xtn
			);
		}
		
		static function mime_of($mime, $xtn){
			$a = explode('/', $mime);
			$xtn = strtolower($xtn);
			if($xtn == 'php') return 'text/html';
			switch($a[0]){
				case 'text':
					switch($xtn){
						case 'php':
						case 'html':
						case 'htm':
							return 'text/html';
						case 'txt':
						case 'log':
							return "text/plain";
						case 'js':
							return 'text/javascript';
						case 'xml':
						case 'css':
							return "text/$xtn";
						default:
							return $mime;
					}
				case 'image':
					switch($xtn){
						case 'jpeg':
						case 'jpg':
							return "image/jpeg";
						case 'gif':
						case 'png':
						case 'bmp':
							return "image/$xtn";
						default:
							return $mime;
					}
			}
			return $mime;
		}
		
		static function class_of($mime, $basename, $xtn){
			// Assume comments and parameters are always stripped before calling this function
			$a = explode('/', $mime);
			switch($a[0]){
				case 'text':
					switch(true){
						case ($basename == '.htaccess'):
							return 'htaccess';
						default:
							return 'text';
					}
				case 'image':
					switch($a[1]){
						case 'jpg':
						case 'jpeg':
						case 'gif':
						case 'png':
						case 'bmp':
							return 'image';
						default:
							return 'file';
					}
				case 'audio':
				case 'video':
					return 'file';
				case 'application':
					switch($xtn){
						case 'tgz':
						case 'tar':
						case 'gz':
							return 'tgz';
					}
					switch($a[1]){
						case 'xhtml+xml':
						case 'javascript':
						case 'json':
							return 'text';
						case 'x-gzip':
							return 'tgz';
						default:
							return 'file';
					}
				case 'multipart':
				case 'message':
					return 'file';
			}
			// This should never happen, but anyway...
			return 'file';
		}
		
	}

?>